import axios from 'axios';
import moment from 'moment';
import { envConfig } from './envConfig.js'
const { url, mock } = envConfig()
const service = axios.create({
    baseURL: url,
    timeout: 1000 * 30,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Access-Control-Allow-Origin': "*"
    }
})
// header信息整理
let getHeader = function (url) {
    let timestamp = moment().format('YYYY-MM-DD hh:mm:ss')
    let token = sessionStorage.getItem("token") || ""
    let source = "web"
    let sign = source + "-" + url + "-" + timestamp + "-" + token
    return {
        'Content-Type': 'application/json; charset=UTF-8; application/x-www-form-urlencoded',
        'Access-Control-Allow-Origin': "*",
        'token': token,
        'timestamp': timestamp,
        'sign': sign,
        'source': source,
    };
}
// 请求拦截
service.interceptors.request.use(
    config => {
        let title = "请求"
        console.log(title, JSON.stringify(config.url), config.params || config.data)
        config.headers = getHeader(url)
        return config;
    }, err => {
        // return 1234;
        return Promise.reject(err);
        // return Promise.reject(err);
    }
)

// 响应拦截
service.interceptors.response.use(
    response => {
        let title = ""
        let configUrl = response.config.url
        let data = response.data
        if (response.status == 200) {
            title = "成功"
        } else {
            title = "失败"
        }
        console.log(title, configUrl, data)
        return response
    },
    error => {
        return Promise.reject(error);
    }
)
export default service;